<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/

	defined('_JEXEC') or die('Restricted access');		
	include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."login.php");	
	include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."check_completed.php");
	
	$document =& JFactory::getDocument();
	$document->addStyleSheet("components/com_surveys/css/survey.css");	
	$document->addScript(JURI::base()."components/com_surveys/javascript/survey_functions.js");
	
	$user =& JFactory::getUser();
	$user_id = $user->id;
	$user_type = $user->groups;
		
	$survey_id_alias = JRequest::getVar("id", "0");
	if($survey_id_alias == "0"){// if we come from a menu, not from a link from list of surveys		
		$survey_id_alias = $this->getSurveyId();
	}
	$survey_id_array = explode(":", $survey_id_alias);
	$survey_id = $survey_id_array["0"];
	$current_page_id = JRequest::getVar("page", "0");
	$session_id = "";
	$show_survey = true;
	$params = "";

	$survey_details = $this->SurveyDetails($survey_id);
	$all_pages = $this->allPages($survey_id);
	
	$item_id = JRequest::getVar("Itemid", "0");
?>

<?php
	if($user_id == "0" && isset($survey_details["0"]) && $survey_details["0"]["access"] != "1"){
		$login = new Login();
		echo $login->getLogin($_SERVER['QUERY_STRING']);
	}
	elseif($user_id != "0" && isset($survey_details["0"]) && $survey_details["0"]["access"] == "3" && !isset($user_type["Super Users"])){
		$login = new Login();
		echo $login->getLogin($_SERVER['QUERY_STRING']);
	}
	else{
		$params = $this->Params();		
		$cc = new CheckCompleted();
		$completed = $cc->check_completed($survey_id, $user_id, $params->general_value);		
		
		if(isset($completed["already_completed"])){
			if($current_page_id != "last"){
				echo '<b>'.$completed["already_completed"].'</b>';
			}
			$show_survey = false;
			
		}
		else{			
			if($current_page_id == "0"){
				$_SESSION["prev_page_array"] = NULL;
				$_SESSION["skip_pages"] = NULL;
				$current_page_id = isset($completed["page_to_view"]) ? intval($completed["page_to_view"]) : "0";				
			}
			if($current_page_id == "last"){
				$show_survey = false;
			}
			$session_id = isset($completed["session_id"]) ? intval($completed["session_id"]) : "";
?>

	<?php
		if($show_survey != false && isset($survey_details["0"])){
	?>
			<!-- survey header, image and description -->
			<table cellspacing="0" cellpadding="0" align="center" class="<?php echo $params->survey_name; ?>">
				<tr>
					<td colspan="1" class="tableh1">
						<table width="100%" cellspacing="0" cellpadding="0" border="0">
							<tr>
								<td class="<?php echo $params->survey_name; ?>"><?php echo $survey_details["0"]["title"]; ?></td>
							</tr>
						</table>
				  </td>
				</tr>
				<tr>    	
					<td class="survey_image">
						<?php
							if(trim($survey_details["0"]["imagelist"]) != ""){
						?>
								<img border="0" alt="" src="images/sampledata/<?php echo $survey_details["0"]["imagelist"]; ?>" />
						<?php
							}
						?>
					</td>
				</tr>
				<?php if(isset($survey_details["0"]) && $survey_details["0"]["description"] != ""){?>				
				<tr>
					<td>			
						<span class="<?php echo $params->survey_description; ?>"><?php echo $survey_details["0"]["description"]; ?></span>
					</td>
				</tr>	
				<?php }//if?>
			</table>	
		
			<!-- page title, image, description -->
			<?php
				if(count($all_pages)>0){			
					if($current_page_id == "0" || $current_page_id == "-1"){
						$current_page_id = $all_pages["0"]["id"];
					}										
					$page_details = $this->pageDetails($current_page_id);		
			?>
				<table>
					<?php 
						//if is set to display page title or no
						if($page_details["0"]["show_title"] == "1"){ 
					?>
					<tr class="<?php echo $params->page_name; ?>">
						<td><?php echo $page_details["0"]["title"]; ?></td>
					</tr>
					<?php 
						}
					?>
					<tr>
						<?php
							if(trim($page_details["0"]["imagelist"]) != ""){
						?>
							<td class="page_image">
								<img alt="" src="images/sampledata/<?php echo $page_details["0"]["imagelist"]; ?>" />
							</td>	
						<?php
							}
						?>
						<td>
							<span class="<?php echo $params->page_description; ?>"><?php echo $page_details["0"]["description"]; ?></span>
						</td>
					</tr>
				</table>
			<?php
				}
			?>	
	<?php
		}//else - already completed
	}//else login
		
	?>
	
	<?php
		$page_order = @$page_details["0"]["ordering"];		
		$pages = $this->getPrevNextPage($survey_id, $current_page_id, $page_order);		
		$prev_page = $pages["prev"];	
		$next_page = $pages["next"];
	?>
		
	<!-- survey conten: pages and question -->	
	<form name="survey_content" id="survey_content" method="post">				
		<?php 			
			if($show_survey === true){				
				echo $this->surveyContent($survey_id, $current_page_id, $params, $completed, $session_id); 
			}
			
			if($current_page_id == "last"){
				echo $this->lastPage($survey_id, $params, $survey_id_alias);
			}
		?>		
		<input type="hidden" name="previews_page_id" value="<?php echo $prev_page; ?>" />
		<input type="hidden" name="current_page_id" value="<?php echo $current_page_id; ?>" />
		<input type="hidden" name="next_page_id" value="<?php echo $next_page; ?>" />
		<input type="hidden" name="task" value=""/>
		<input type="hidden" name="session_id" value="<?php echo $session_id; ?>" />
		<?php
			if($session_id != ""){
				$_SESSION["session_id"] = $session_id;
			}
		?>
		<input type="hidden" name="id" value="<?php echo $survey_id; ?>" />
		<input type="hidden" name="survey_id_alias" value="<?php echo $survey_id_alias; ?>" />
		<input type="hidden" name="option" value="com_surveys"/>
		<input type="hidden" name="controller" value="editsurvey"/>
		<input type="hidden" name="Itemid" value="<?php echo intval($item_id); ?>"/>
	</form>
	
<?php 
	}//else 
?>